import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Hot from '../views/hot/Hot.vue'
import Me from '../views/me/Me.vue'
import News from '../views/news/News.vue'
import Home from '../views/home/Home.vue'
const routes = [
  {
    path: '/',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/Home.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/hot',
    name: 'hot',
    component: () => import('../views/hot/Hot.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/me',
    name: 'me',
    component: () => import('../views/me/Me.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/mecont',
    name: 'mecont',
    component: () => import('../views/me/MeCont.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/news',
    name: 'news',
    component: () => import('../views/news/News.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/newszan',
    name: 'newszan',
    component: () => import('@/views/news/NewsZan.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/newsfollow',
    name: 'newsfollow',
    component: () => import('@/views/news/NewsFollow.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/newscomment',
    name: 'newscomment',
    component: () => import('@/views/news/NewsComment.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/publish',
    name: 'publish',
    component: () => import('../views/publish/Publish.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../components/hot/search.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/market',
    name: 'market',
    component: () => import('../views/home/Market.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/market/detail/:id',
    name: 'market-detail',
    component: () => import('../views/home/MarketDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/market/comment/:id',
    name: 'market-comment',
    component: () => import('../views/home/MarketComment.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/market/cart',
    name: 'market-cart',
    component: () => import('../views/home/MarketCart.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/upme',
    name: 'upme',
    component: () => import('../views/me/UpMe.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/information',
    name: 'information',
    component: () => import('../views/news/Information.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/market/cart/setting',
    name: 'market-cart-setting',
    component: () => import('../views/home/MarketCartSetting.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/userlist',
    name: 'userlist',
    component: () => import('../views/me/UserList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/usercont',
    name: 'usercont',
    component: () => import('../views/me/UserCont.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/setting',
    name: 'setting',
    component: () => import('../views/home/MarketCartSetting.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/me_setting',
    name: 'me_setting',
    component: () => import('../views/me/Setting.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/setting/address-edit',
    name: 'address-edit',
    component: () => import('../views/setting/AddressEdit.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/home/Order.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/search-result',
    name: 'search-result',
    component: () => import('../views/search/SearchResult.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user-search-result',
    name: 'user-search-result',
    component: () => import('../views/search/UserSearchResult.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 获取本地存储的user_id
  const userId = localStorage.getItem('user_id')

  // 检查路由是否需要登录验证
  if (to.meta.requiresAuth) {
    // 检查user_id是否存在且不为空
    if (!userId || userId === '') {
      // 如果没有user_id或为空，重定向到登录页
      next('/')
    } else {
      // 如果有user_id，允许访问
      next()
    }
  } else {
    // 不需要登录验证的路由（登录页面）
    if (userId && userId !== '') {
      // 如果已经登录，访问登录页面时重定向到home页
      next('/home')
    } else {
      // 未登录用户访问登录页面，直接放行
      next()
    }
  }
})

export default router